package com.growthserver.controller;


import com.github.pagehelper.PageInfo;
import com.growthserver.enums.ResultBeans;
import com.growthserver.entity.TIntegralInfo;
import com.growthserver.entity.TMemberEntity;
import com.growthserver.entity.TTaskRuleEntity;
import com.growthserver.param.CommonParam;
import com.growthserver.param.IntegralParam;
import com.growthserver.service.AppletService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping(value = "/mp/portal/growth/externalInterface", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
@Api(description = "对外接口")
public class AppletController {

    @Autowired
    private AppletService appletService;

    /**
     * 用户会员信息注册
     */
    @ApiOperation(value = "用户会员信息注册", notes = "对外调用接口")
    @PostMapping("/memberRegistration")
//    @ApisInfo(apis = {
//            @GwApiDesc(name = "ly.mp.portal.growth.memberManagement.memberRegistration", type = ApiType.PORTAL),
//            @GwApiDesc(name = "ly.mp.inner.growth.memberManagement.memberRegistration", type = ApiType.INNER_SERVER)
//    }, remark = "对外接口--用户会员信息注册", qps = 100, connectTimeout = 5)
    public ResultBeans<Object> memberRegistration(@RequestHeader(required = false, name = "uid") String uid,
                                                  @RequestHeader(required = false, name = "userName") String userName,
                                                  @RequestBody CommonParam commonParam) {
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            appletService.memberRegistration(commonParam.getUserId(),commonParam.getUserName());
            result.setResult("1");
            result.setMsg("用户会员信息注册成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("用户会员信息注册失败");
        }
        return result;
    }

    /**
     * 用户会员信息注销
     */
    @ApiOperation(value = "用户会员信息注销", notes = "对外调用接口")
    @PostMapping("/memberLogOff")
//    @ApisInfo(apis = {
//            @GwApiDesc(name = "ly.mp.portal.growth.memberManagement.memberLogOff", type = ApiType.PORTAL),
//            @GwApiDesc(name = "ly.mp.inner.growth.memberManagement.memberLogOff", type = ApiType.INNER_SERVER)
//    }, remark = "对外接口--用户会员信息注销", qps = 100, connectTimeout = 5)
    public ResultBeans<Object> memberLogOff(@RequestHeader(required = false, name = "uid") String uid,
                                                  @RequestHeader(required = false, name = "userName") String userName,
                                                  @RequestBody CommonParam commonParam) {
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            appletService.memberLogOff(commonParam.getUserId());
            result.setResult("1");
            result.setMsg("用户会员信息注销成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("用户会员信息注销失败");
        }
        return result;
    }

    /**
     * 会员用户类型变更
     */
    @ApiOperation(value = "会员用户类型变更", notes = "对外调用接口")
    @PostMapping("/updateUserType")
//    @ApisInfo(apis = {
//            @GwApiDesc(name = "ly.mp.portal.growth.memberManagement.updateUserType", type = ApiType.PORTAL),
//            @GwApiDesc(name = "ly.mp.inner.growth.memberManagement.updateUserType", type = ApiType.INNER_SERVER)
//    }, remark = "对外接口--会员用户类型变更", qps = 100, connectTimeout = 5)
    public ResultBeans<Object> updateUserType(@RequestHeader(required = false, name = "uid") String uid,@RequestBody CommonParam commonParam) {
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            appletService.updateUserType(uid,commonParam.getUserType());
            result.setResult("1");
            result.setMsg("会员用户类型变更成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("会员用户类型变更失败");
        }
        return result;
    }


    /**
     * 获取用户会员信息
     */
    @ApiOperation(value = "获取用户会员信息",notes = "对外调用接口")
    @PostMapping("/getUserLevel")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.portal.growth.memberManagement.getUserLevel", type = ApiType.PORTAL),
//            @GwApiDesc(name = "ly.mp.inner.growth.memberManagement.getUserLevel", type = ApiType.INNER_SERVER),
//            @GwApiDesc(name = "ly.mp.app.h5.growth.memberManagement.getUserLevel", type = ApiType.APP_H5),
//            @GwApiDesc(name = "ly.mp.miniprogram.growth.memberManagement.getUserLevel", type = ApiType.MINI_PROGRAM)
//    }, remark = "对外接口--获取用户会员信息", qps = 100, connectTimeout = 5)
    public ResultBeans<TMemberEntity> getUserLevel(@RequestHeader(required = false, name = "uid") String uid,
                                                   @RequestBody CommonParam commonParam){
        ResultBeans<TMemberEntity> result = new ResultBeans<>();
        try {
            TMemberEntity userLevel = appletService.getUserLevel(commonParam.getUserId());
            result.setRows(userLevel);
            result.setResult("1");
            result.setMsg("获取用户等级,总成长值成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("获取用户等级,总成长值失败");
        }
        return result;
    }

    /**
     * 获取用户会员信息集合
     */
    @ApiOperation(value = "获取用户会员信息集合",notes = "对外调用接口")
    @PostMapping("/getUserLevelList")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.portal.growth.memberManagement.getUserLevelList", type = ApiType.PORTAL),
//            @GwApiDesc(name = "ly.mp.inner.growth.memberManagement.getUserLevelList", type = ApiType.INNER_SERVER),
//            @GwApiDesc(name = "ly.mp.app.h5.growth.memberManagement.getUserLevelList", type = ApiType.APP_H5),
//            @GwApiDesc(name = "ly.mp.miniprogram.growth.memberManagement.getUserLevelList", type = ApiType.MINI_PROGRAM)
//    }, remark = "对外接口--获取用户会员信息集合", qps = 100, connectTimeout = 5)
    public ResultBeans<List<HashMap<String, String>>> getUserLevelList(@RequestHeader(required = false, name = "uid") String uid,
                                                 @RequestBody CommonParam commonParam){
        ResultBeans<List<HashMap<String, String>>> result = new ResultBeans<>();
        try {
            List<HashMap<String, String>> userLevelList = appletService.getUserLevelList(commonParam.getUserIds());
            result.setRows(userLevelList);
            result.setResult("1");
            result.setMsg("获取用户会员信息集合成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("获取用户会员信息集合失败");
        }
        return result;
    }

    /**
     * 获取用户成长值明细
     */
    @ApiOperation(value = "获取用户成长值明细",notes = "对外调用接口")
    @PostMapping("/getUserGrowth")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.portal.growth.memberManagement.getUserGrowth", type = ApiType.PORTAL),
//            @GwApiDesc(name = "ly.mp.miniprogram.growth.memberManagement.getUserGrowth", type = ApiType.MINI_PROGRAM)
//    }, remark = "对外接口--获取用户成长值明细", qps = 100, connectTimeout = 5)
    public ResultBeans<List<TIntegralInfo>> getUserGrowth(@RequestHeader(required = false, name = "uid") String uid, @RequestBody CommonParam commonParam){
        ResultBeans<List<TIntegralInfo>> result = new ResultBeans<>();
        try {
            commonParam.setUserId(uid);
            List<TIntegralInfo> userGrowth = appletService.getUserGrowth(commonParam);
            PageInfo<TIntegralInfo> tIntegralInfoPageInfo = new PageInfo<>(userGrowth);
            result.setRows(userGrowth);
            result.setResult("1");
            result.setTotal((int)tIntegralInfoPageInfo.getTotal());
            result.setMsg("获取用户成长值明细成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("获取用户成长值明细失败");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

    /**
     * 获取用户总成长值
     */
    @ApiOperation(value = "获取用户总成长值",notes = "对外调用接口")
    @PostMapping("/getTotalGrowthValue")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.portal.growth.memberManagement.getTotalGrowthValue", type = ApiType.PORTAL),
//            @GwApiDesc(name = "ly.mp.miniprogram.growth.memberManagement.getTotalGrowthValue", type = ApiType.MINI_PROGRAM)
//    }, remark = "对外接口--获取用户总成长值", qps = 100, connectTimeout = 5)
    public ResultBeans<Object> getTotalGrowthValue(@RequestHeader(required = false, name = "uid") String uid,
            @RequestBody CommonParam commonParam){
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            String userLevel = appletService.getTotalGrowthValue(uid);
            result.setRows(userLevel);
            result.setResult("1");
            result.setMsg("获取用户总成长值成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("获取用户总成长值失败");
        }
        return result;
    }

    /**
     * 获取用户等级对应权益
     */
    @ApiOperation(value = "获取用户等级对应权益",notes = "对外调用接口")
    @PostMapping("/getUserLevelEquity")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.portal.growth.memberManagement.getUserLevelEquity", type = ApiType.PORTAL),
//            @GwApiDesc(name = "ly.mp.miniprogram.growth.memberManagement.getUserLevelEquity", type = ApiType.MINI_PROGRAM),
//            @GwApiDesc(name = "ly.mp.h5.growth.memberManagement.getUserLevelEquity", type = ApiType.APP_H5)
//    }, remark = "对外接口--获取用户等级对应权益", qps = 100, connectTimeout = 5)
    public Object getUserLevelEquity(@RequestHeader(required = false, name = "uid") String uid,@RequestBody CommonParam commonParam){
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            Object userLevelEquity = appletService.getUserLevelEquity(uid);
            result.setRows(userLevelEquity);
            result.setResult("1");
            result.setMsg("获取用户等级对应权益成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("获取用户等级对应权益失败");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

    /**
     * 获取用户等级成长值(滑动)
     */
    @ApiOperation(value = "获取用户等级成长值(滑动)",notes = "对外调用接口")
    @PostMapping("/equityGrowthSliding")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.portal.growth.memberManagement.equityGrowthSliding", type = ApiType.PORTAL),
//            @GwApiDesc(name = "ly.mp.miniprogram.growth.memberManagement.equityGrowthSliding", type = ApiType.MINI_PROGRAM),
//            @GwApiDesc(name = "ly.mp.h5.growth.memberManagement.equityGrowthSliding", type = ApiType.APP_H5),
//    }, remark = "对外接口--获取用户等级成长值(滑动)", qps = 100, connectTimeout = 5)
    public Object equityGrowthSliding(@RequestHeader(required = false, name = "uid") String uid,@RequestBody CommonParam commonParam){
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            Object userLevelEquity = appletService.equityGrowthSliding(uid);
            result.setRows(userLevelEquity);
            result.setResult("1");
            result.setMsg("获取用户等级成长值(滑动)成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("获取用户等级成长值(滑动)失败");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

    /**
     * 获取用户积分明细
     */
    @ApiOperation(value = "获取用户积分明细",notes = "对外调用接口")
    @PostMapping("/getUserIntegral")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.portal.growth.memberManagement.getUserIntegral", type = ApiType.PORTAL),
//            @GwApiDesc(name = "ly.mp.miniprogram.growth.memberManagement.getUserIntegral", type = ApiType.MINI_PROGRAM)
//    }, remark = "对外接口--获取用户积分明细", qps = 100, connectTimeout = 5)
    public Object getUserIntegral(@RequestHeader(required = false, name = "uid") String uid,@RequestBody CommonParam commonParam){
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            commonParam.setUserId(uid);
            List<TIntegralInfo> userIntegral = appletService.getUserIntegral(commonParam);
            PageInfo<TIntegralInfo> tIntegralInfoPageInfo = new PageInfo<>(userIntegral);
            result.setRows(userIntegral);
            result.setTotal((int)tIntegralInfoPageInfo.getTotal());
            result.setResult("1");
            result.setMsg("获取用户积分明细成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("获取用户积分明细失败");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

    /**
     * 获取用户即将过期积分
     */
    @ApiOperation(value = "获取用户即将过期积分",notes = "对外调用接口")
    @PostMapping("/getWillExpIntegral")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.portal.growth.memberManagement.getWillExpIntegral", type = ApiType.PORTAL),
//            @GwApiDesc(name = "ly.mp.miniprogram.growth.memberManagement.getWillExpIntegral", type = ApiType.MINI_PROGRAM)
//    }, remark = "对外接口--获取用户即将过期积分", qps = 100, connectTimeout = 5)
    public Object getWillExpIntegral(@RequestHeader(required = false, name = "uid") String uid,@RequestBody CommonParam commonParam){
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            String willExpIntegral = appletService.getWillExpIntegral(uid);
            result.setRows(willExpIntegral);
            result.setResult("1");
            result.setMsg("获取用户即将过期积分成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("获取用户即将过期积分失败");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

    /**
     * 获取用户总积分
     */
    @ApiOperation(value = "获取用户总积分",notes = "对外调用接口")
    @PostMapping("/getTotalIntegral")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.portal.growth.memberManagement.getTotalIntegral", type = ApiType.PORTAL),
//            @GwApiDesc(name = "ly.mp.miniprogram.growth.memberManagement.getTotalIntegral", type = ApiType.MINI_PROGRAM)
//    }, remark = "对外接口--获取用户总积分", qps = 100, connectTimeout = 5)
    public ResultBeans<Object> getTotalIntegral(@RequestHeader(required = false, name = "uid") String uid,@RequestBody CommonParam commonParam){
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            String totalIntegral = appletService.getTotalIntegral(uid);
            result.setRows(totalIntegral);
            result.setResult("1");
            result.setMsg("获取用户总积分成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("获取用户总积分失败");
        }
        return result;
    }

    /**
     *  对外调用接口  被动
     *  签到,点赞,抽奖.....
     */
    @ApiOperation(value = "对外调用接口--签到,点赞,抽奖.....",notes = "对外调用接口")
    @PostMapping("/growthRules")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.inner.growth.memberManagement.growthRules", type = ApiType.INNER_SERVER)
//    }, remark = "对外接口--对外调用接口--签到,点赞,抽奖.....", qps = 100, connectTimeout = 5)
    public Object growthRules(@RequestBody IntegralParam integralParam){
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            List<HashMap<String, String>> hashMapList = appletService.growthRules(integralParam);
            result.setRows(hashMapList);
            result.setResult("1");
            result.setMsg("对外调用接口--签到,点赞,抽奖.....成功");
        } catch (Exception e) {
            result.setRows(new ArrayList<>());
            result.setResult("0");
            result.setMsg("对外调用接口--签到,点赞,抽奖.....失败");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

    /**
     *  活动任务规则时间获取
     */
    @ApiOperation(value = "活动任务规则时间获取",notes = "对外调用接口")
    @PostMapping("/taskRuleInActiveTime")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.inner.growth.memberManagement.taskRuleInActiveTime", type = ApiType.INNER_SERVER)
//    }, remark = "对外接口--活动任务规则时间获取", qps = 100, connectTimeout = 5)
    public Object taskRuleInActiveTime(@RequestBody IntegralParam integralParam){
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            List<TTaskRuleEntity> list = appletService.taskRuleInActiveTime(integralParam);
            result.setRows(list);
            result.setResult("1");
            result.setMsg("活动任务规则时间获取成功");
        } catch (Exception e) {
            result.setRows(new ArrayList<>());
            result.setResult("0");
            result.setMsg("活动任务规则时间获取失败");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

    /**
     *  积分预算未使用查询
     *  integralBudget
     */
    @ApiOperation(value = "积分预算未使用查询",notes = "对外调用接口")
    @PostMapping("/getIntegralBudgetNo")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.portal.growth.memberManagement.getIntegralBudgetNo", type = ApiType.PORTAL)
//    }, remark = "对外接口--积分预算未使用查询", qps = 100, connectTimeout = 5)
    public Object getIntegralBudgetNo(@RequestHeader(required = false, name = "uid") String uid,@RequestBody IntegralParam integralParam){
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            List<HashMap<String, String>> integralBudgetNo = appletService.getIntegralBudgetNo(integralParam);
            result.setResult("1");
            result.setRows(integralBudgetNo);
            result.setMsg("积分预算未使用查询成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("积分预算未使用查询失败");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

    /**
     *  积分预算发布
     *  integralBudget
     */
    @ApiOperation(value = "积分预算发布",notes = "对外调用接口")
    @PostMapping("/integralBudgetRelease")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.inner.growth.memberManagement.integralBudgetRelease", type = ApiType.INNER_SERVER),
//    }, remark = "对外接口--积分预算发布", qps = 100, connectTimeout = 5)
    public Object integralBudgetRelease(@RequestHeader(required = false, name = "uid") String uid,@RequestBody IntegralParam integralParam){
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            appletService.integralBudgetRelease(integralParam);
            result.setResult("1");
            result.setMsg("积分预算发布成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("积分预算发布失败");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

    /**
     *  积分预算单号剩余积分查询
     *  integralBudget
     */
    @ApiOperation(value = "积分预算单号剩余积分查询",notes = "对外调用接口")
    @PostMapping("/getIntegralBudgetNoIntegral")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.inner.growth.memberManagement.getIntegralBudgetNoIntegral", type = ApiType.INNER_SERVER)
//    }, remark = "对外接口--积分预算单号剩余积分查询", qps = 100, connectTimeout = 5)
    public Object getIntegralBudgetNoIntegral(@RequestBody IntegralParam integralParam){
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            int integralBudgetNoIntegral = appletService.getIntegralBudgetNoIntegral(integralParam);
            result.setRows(integralBudgetNoIntegral);
            result.setResult("1");
            result.setMsg("积分预算单号剩余积分查询成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("积分预算单号剩余积分查询失败");
            result.setRows(new ArrayList<>());
        }
        return result;
    }

    /**
     *  签到广场
     *  integralBudget
     */
    @ApiOperation(value = "签到广场",notes = "对外调用接口")
    @PostMapping("/signInSquare")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.inner.growth.memberManagement.signInSquare", type = ApiType.INNER_SERVER)
//    }, remark = "对外接口--签到广场", qps = 100, connectTimeout = 5)
    public Object signInSquare(@RequestHeader(required = false, name = "uid") String uid,@RequestBody IntegralParam integralParam){
        ResultBeans<Object> result = new ResultBeans<>();
        try {
            integralParam.setUserId(uid);
            Object integralBudgetNoIntegral = appletService.signInSquare(integralParam);
            result.setRows(integralBudgetNoIntegral);
            result.setResult("1");
            result.setMsg("签到广场查询成功");
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("签到广场查询失败");
            result.setRows(new ArrayList<>());
        }
        return result;
    }


    @ApiOperation(value = "对外调用接口--根据等级获取会员车主信息",notes = "获取会员车主信息")
    @PostMapping("/selectMembersByLevel.do")
//    @ApisInfo(apis= {
//            @GwApiDesc(name = "ly.mp.inner.memberManagement.selectMembersByLevel", type = ApiType.INNER_SERVER)
//    }, remark = "对外接口--根据等级获取会员车主信息.....", qps = 1000, connectTimeout = 5)
    public ResultBeans<List<TMemberEntity>> selectMembersByLevel(@RequestBody IntegralParam integralParam){
        ResultBeans<List<TMemberEntity>> result = new ResultBeans<>();
        try {
            List<TMemberEntity> tMemberEntities = appletService.selectMembersByLevel(integralParam);
            result.setResult("1");
            result.setMsg("success");
            result.setRows(tMemberEntities);
        } catch (Exception e) {
            result.setResult("0");
            result.setMsg("接口请求失败");
        }
        return result;
    }
}
